package pooky.server;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import pooky.client.DuplicateKeyException;
import pooky.client.NotLoggedInException;
import pooky.client.PookyStopService;
import pooky.shared.PookyStop;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class PookyStopServiceImpl  extends RemoteServiceServlet implements PookyStopService {

	private static final Logger LOG = Logger.getLogger(PookyStopServiceImpl.class.getName());
	
	
	@Override
	public void addPookyStop(PookyStop pookystop) throws NotLoggedInException, DuplicateKeyException {
		 	checkLoggedIn();
		 	checkUniqueLocation(pookystop);
			
		    PersistenceManager pm = getPersistenceManager();
		    try {
		      pm.makePersistent(pookystop);
		    } finally {
		      pm.close();
		    }
	}

	@Override
	public void removePookyStop(PookyStop pookystop) throws NotLoggedInException {
		checkLoggedIn();
	    PersistenceManager pm = getPersistenceManager();
	    try {
	      long deleteCount = 0;
	      Query q = pm.newQuery(PookyStop.class);
	      List<PookyStop> pookyStopList = (List<PookyStop>) q.execute();
	      for (PookyStop ps : pookyStopList) {
	        if (pookystop.getLocation().equals(ps.getLocation())) {
	          deleteCount++;
	          pm.deletePersistent(ps);
	        }
	      }
	      if (deleteCount != 1) {
	        LOG.log(Level.WARNING, "removeStock deleted "+deleteCount+" Stocks");
	      }
	    } finally {
	      pm.close();
	    }
	}

	@Override
	public PookyStop[] getPookyStops() throws NotLoggedInException {
		checkLoggedIn();
	    PersistenceManager pm = getPersistenceManager();
	    List<PookyStop> pookyStopList = new ArrayList<PookyStop>();
	    
	    try {
	      Query q = pm.newQuery(PookyStop.class);
	      q.setOrdering("date");
	      List<PookyStop> psquerylist = (List<PookyStop>) q.execute();
	      int i =0;
	      for (PookyStop ps : psquerylist) {
	    	  pookyStopList.add(ps);
	    	  i++;
	      }
	    } finally {
	      pm.close();
	    }
	    
	    PookyStop []pArray = new PookyStop[pookyStopList.size()];
	    pookyStopList.toArray(pArray);
	    
	    return pArray;
	}
	
	
	private void checkLoggedIn() throws NotLoggedInException {
	    if (getUser() == null) {
	      throw new NotLoggedInException("Not logged in.");
	    }
	  }

	private void checkUniqueLocation(PookyStop ps) throws DuplicateKeyException {
		if (Utility.doesEntityExist(PookyStop.class, ps.getLocation())){
			throw new DuplicateKeyException(ps.getLocation());
		}
	}
	
	
	  private User getUser() {
	    UserService userService = UserServiceFactory.getUserService();
	    return userService.getCurrentUser();
	  }

	  private PersistenceManager getPersistenceManager() {
	    return Utility.getPersistenceManager();
	  }



	  
}
